#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>

#define PORT 8080

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

    // 创建服务器套接字
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    // 绑定套接字到端口
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    // 开始监听连接请求
    if (listen(server_fd, 3) < 0) {
        perror("listen failed");
        exit(EXIT_FAILURE);
    }

    std::cout << "TCP Server waiting for client connections..." << std::endl;

    // 接受客户端连接（阻塞调用）
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
        perror("accept failed");
        exit(EXIT_FAILURE);
    }

    std::cout << "Connected to client!" << std::endl;

    // 接收客户端请求数据（阻塞调用）
    int valread = read(new_socket, buffer, 1024);
    (void)valread;
    std::cout << "Received from client: " << buffer << std::endl;

    // 回复客户端响应
    const char *response = "Hello from server!";
    send(new_socket, response, strlen(response), 0);
    std::cout << "Response sent to client." << std::endl;

    // 关闭套接字
    close(new_socket);
    close(server_fd);

    return 0;
}
